package com.heima.mapper;

import com.github.pagehelper.Page;
import com.heima.dto.pagedto.ReportPageDto;
import com.heima.vo.ContractVo;
import com.heima.vo.reportvo.*;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDate;
import java.util.List;

@Mapper
public interface ReportMapper {
    /**
     * 查询合同基础数据
     * @param dto
     * @return
     */
    Page<ContractReportVo> queryContractList(ReportPageDto dto);

    /**
     * 统计合同的新增和总数
     * @param begin
     * @param localDate
     * @return
     */
    List<CountVo> contractCount(LocalDate begin, LocalDate localDate);

    /**
     * 获取销售报表数据
     * @return
     */
    Page<SalesReportVo> getSalesReport(LocalDate beginDate, LocalDate endDate);

    /**
     * 统计合同数量
     * @param begin
     * @param end
     * @return
     */
    @Select("SELECT DATE(create_time) AS create_time, sum(new_count) AS newCount,sum(total_count) as totalCount FROM tb_contract_count WHERE create_time BETWEEN #{begin} AND #{end} GROUP BY DATE(create_time)")
    List<CountVo> getCountList(LocalDate begin, LocalDate end);

    /**
     * 统计线索数量
     * @param begin
     * @param end
     * @return
     */
    @Select("SELECT DATE(create_time) AS create_time, sum(new_count) AS newCount,sum(total_count) as totalCount FROM tb_clue_count WHERE create_time BETWEEN #{begin} AND #{end} GROUP BY DATE(create_time)")
    List<CountVo> getClueCountList(LocalDate begin, LocalDate end);

    /**
     * 活动饼图
     * @param begin
     * @param end
     * @return
     */
    @Select("SELECT name AS activity, COUNT(*) AS num FROM tb_contract WHERE create_time BETWEEN #{begin} AND #{end} GROUP BY activity_id")
    List<ActivityNum> findActivityNumList(LocalDate begin, LocalDate end);

    /**
     * 线索统计
     * @param dto
     * @return
     */
    Page<ClueVo> queryClueList(ReportPageDto dto);

    /**
     * 销售统计
     * @param begin
     * @param end
     * @return
     */
    @Select("SELECT sum(`order`)  FROM tb_contract WHERE create_time BETWEEN #{begin} AND #{end} GROUP BY create_time")
    List<Double> findSaleVo(LocalDate begin, LocalDate end);
}
